package com.yanqu.road.server.cmd.banquet.crossBanquet;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.UserCrossBanquetData;
import com.yanqu.road.entity.banquet.cross.CrossBanquetMember;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.BanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.Map;

@Cmd(code = Protocol.S_BANQUET_CROSS_MEMBER_LIST_PAGE, desc = "请求宴会成员列表")
public class GetBanquetMemberPageCmd extends AbstractCommand {

	private String banquetId;

	private int groupNum = 0;
	private int groupId = 0;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		BanquetProto.BanquetMemberInfoReq reqMsg = BanquetProto.BanquetMemberInfoReq.parseFrom(packet.getBytes());
		banquetId = reqMsg.getBanquetId();
		groupNum = reqMsg.getGroupNum();
		groupId = reqMsg.getGroupId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(CrossBanquetMgr.isBanquetOver(banquetId)){
			ret = GameErrorCode.E_BANQUET_IS_OVER;
		}
		if(groupNum < 100 || groupNum > 500){
			ret = GameErrorCode.E_BANQUET_MAX_SEAT;
		}

		if(groupId < 1){
			ret = GameErrorCode.E_BANQUET_MAX_SEAT;
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		CrossBanquetProto.CrossBanquetMemberInfoResp.Builder respMsg = CrossBanquetProto.CrossBanquetMemberInfoResp.newBuilder();
		if(ret != 0){
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_BANQUET_CROSS_MEMBER_LIST_PAGE, respMsg);
			return;
		}
		//进入宴会场景
		CrossBanquetMgr.addBanquetScene(player.getUserId(),banquetId);
		//分批发送宴会成员，第一次通过协议返回，后面通过同步返回
		UserCrossBanquetData banquet = CrossBanquetMgr.getBanquet(banquetId);
		Map<Long, CrossBanquetMember> crossBanquetMemberMap = CrossBanquetMgr.getBanquetMemberMap(banquetId,groupNum,groupId);
		respMsg = BanquetPb.parseBanquetMemberListRespMsg(player,banquet,crossBanquetMemberMap,banquet.getUserCount());
		respMsg.setRet(ret);
		respMsg.setCount(banquet.getUserCount());
		ActivityInfo crbAcInfo = NormalActivityMgr.getRunningCrossBanquetRankActivity();
		BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(banquet.getType());
		if (crbAcInfo == null || banquetInfo.getType1() != 5) {
			respMsg.setAcId(0);
		}else {
			//要知道每个宴会开的人是不是设置了自动回宴会，要去跨服走一趟
			respMsg.setAcId(crbAcInfo.getActivityId());
			YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_BANQUET_AUTO_MEMBER_LIST_PAGE, respMsg);
			GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
			return;
		}
		player.sendPacket(Protocol.U_BANQUET_CROSS_MEMBER_LIST_PAGE, respMsg);
	}
}
